package com.oracle.lingshangejia.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;
//转入
public class RollInActivity extends BaseActivity implements View.OnClickListener {

    private InputMethodManager manager;
    private RelativeLayout re_back;
    private RelativeLayout re_date;
    private TextView tv_date;
    private EditText et_roll_unit;//转入单位
    private EditText et_roll_result;//检查结果
    private EditText et_roll_yj;//方案意见
    private EditText et_roll_doctor;//转诊医生
    private EditText et_roll_phone;//电话
    private EditText et_roll_jg;//当前机构名称
    private RelativeLayout rl_rollin_ok;//确定转入
    private int jmid,jzid;
    @Override
    public int intiLayout() {
        return R.layout.activity_roll_in;
    }

    @Override
    public void initView() {
        jmid=getIntent().getIntExtra("jmid",0);
        jzid=getIntent().getIntExtra("jzid",0);
        manager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        re_back = findViewById(R.id.re_back);
        re_back.setOnClickListener(this);
        re_date = findViewById(R.id.re_date);
        re_date.setOnClickListener(this);
        et_roll_jg = findViewById(R.id.et_roll_jg);
        et_roll_unit=findViewById(R.id.et_roll_unit);
        et_roll_result=findViewById(R.id.et_roll_result);
        et_roll_yj=findViewById(R.id.et_roll_yj);
        et_roll_doctor=findViewById(R.id.et_roll_doctor);
        et_roll_phone=findViewById(R.id.et_roll_phone);
        rl_rollin_ok=findViewById(R.id.rl_rollin_ok);
        rl_rollin_ok.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {

        if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
            manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        switch (v.getId()){

            //返回键
            case R.id.re_back:

                finish();
                break;
            case R.id.rl_rollin_ok:  //确定
                String unit=et_roll_unit.getText().toString().trim();
                if(TextUtils.isEmpty(unit)){
                    Toast.makeText(RollInActivity.this,"请输入转入单位",Toast.LENGTH_SHORT).show();
                    return;
                }
                String result=et_roll_result.getText().toString().trim();
                if(TextUtils.isEmpty(result)){
                    Toast.makeText(RollInActivity.this,"请输入转入原因",Toast.LENGTH_SHORT).show();
                    return;
                }
                String yj=et_roll_yj.getText().toString().trim();
                if(TextUtils.isEmpty(yj)){
                    Toast.makeText(RollInActivity.this,"请输入意见及方案",Toast.LENGTH_SHORT).show();
                    return;
                }
                String doctor=et_roll_doctor.getText().toString().trim();
                if(TextUtils.isEmpty(doctor)){
                    Toast.makeText(RollInActivity.this,"请输入确诊医生",Toast.LENGTH_SHORT).show();
                    return;
                }
                String phone=et_roll_phone.getText().toString().trim();
                if(TextUtils.isEmpty(phone)){
                    Toast.makeText(RollInActivity.this,"请输入联系电话",Toast.LENGTH_SHORT).show();
                    return;
                }
                String date=et_roll_jg.getText().toString().trim();
                if(TextUtils.isEmpty(date)){
                    Toast.makeText(RollInActivity.this,"请输入当前机构名称",Toast.LENGTH_SHORT).show();
                    return;
                }
                getNetWork(unit,result,yj,doctor,phone,date);
                break;
        }
    }

    private void getNetWork(String unit, String result, String yj, String doctor, String phone, String date) {
        int ysid= (int) SpUtils.get(RollInActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("jmId",jmid);
        map.put("jzId",jzid);
        map.put("zrjgmc",unit);
        map.put("jcjg",result);
        map.put("fajy",yj);
        map.put("lxdh",phone);
        map.put("zzysId",ysid);
        map.put("dqjgmc",date);
        OkHttpUtils.getNetWork(RollInActivity.this, Constant.baseurl+"online/jzzz/sxzzzr",map,handler);
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    DataBean bean=gson.fromJson(s, DataBean.class);
                    if(bean.getCode()==0){
                        Toast.makeText(RollInActivity.this,"转入成功",Toast.LENGTH_SHORT).show();
                        finish();
                    }else{
                        Toast.makeText(RollInActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(RollInActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(RollInActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(RollInActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN){

            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}
